package vip.xiaonuo.bi.core.datasource.util;

/**
 * @auther: 顾育司
 * @Description 数据大小换算计算
 * @date: 2024/1/13
 */
public class DatasizeUtils {

    /**
     * 计算数据量大小换算
     * @param size
     * @return
     */
    public static String calculateSize(long size) {
        double result;
        String unit;

        if (size >= 1024 * 1024 * 1024) {
            result = (double) size / (1024 * 1024 * 1024);
            unit = "G";
        } else if (size >= 1024 * 1024) {
            result = (double) size / (1024 * 1024);
            unit = "M";
        } else if (size >= 1024) {
            result = (double) size / 1024;
            unit = "K";
        } else {
            result = (double) size;
            unit = "B";
        }

        return String.format("%.2f", result) + unit;
    }

    public static void main(String[] args) {
        System.out.println(calculateSize(1L));
    }

}
